package com.junior.fixclub.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.junior.fixclub.R;
import com.junior.fixclub.activity.DevicesActivity;
import com.junior.fixclub.model.DeviceInfo;

import java.util.HashMap;
import java.util.List;

public class DeviceAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private Context context;
    private List<DeviceInfo> list;
    HashMap<String,Boolean> states=new HashMap<String,Boolean>();

    public DeviceAdapter(List<DeviceInfo> list, DevicesActivity activity) {
        this.list = list;
        this.context = activity;
        layoutInflater = LayoutInflater.from(context);
    }

    public void onDateChange(List<DeviceInfo> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.device_item, null);
            viewHolder.brandView = convertView.findViewById(R.id.dv_item_brand);
            viewHolder.modelView = convertView.findViewById(R.id.dv_item_model);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final RadioButton radioBtn= convertView.findViewById(R.id.dv_item_choice);
        viewHolder.radioButton = radioBtn;

        DeviceInfo deviceInfo = list.get(position);
        viewHolder.brandView.setText(deviceInfo.getDeviceBrand());
        viewHolder.modelView.setText(deviceInfo.getDeviceModel());
        viewHolder.radioButton.setText(deviceInfo.getDeviceBrand()+"#"+deviceInfo.getDeviceModel());

        viewHolder.radioButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                //重置，确保最多只有一项被选中
                for(String key:states.keySet()){
                    states.put(key, false);

                }
                states.put(String.valueOf(position),radioBtn.isChecked());
                DeviceAdapter.this.notifyDataSetChanged();
            }
        });

        boolean res = false;
        if(states.get(String.valueOf(position)) == null || states.get(String.valueOf(position))== false){
            res = false;
            states.put(String.valueOf(position), false);
        }else {
            res = true;
        }
        viewHolder.radioButton.setChecked(res);
        return convertView;
    }


    public class ViewHolder {
        public TextView brandView;
        public TextView modelView;
        public RadioButton radioButton;
    }
}
